package com.next.aappublicapp.views;

import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.next.aap.client.BaseItem;
import com.next.aappublicapp.R;
import com.next.aappublicapp.handlers.ItemClickHandler;
import com.next.aappublicapp.tasks.DownloadImageTask;
import com.next.aappublicapp.util.ImageUtil;
import com.nostra13.universalimageloader.cache.disc.DiscCacheAware;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class ImageTextView<T extends BaseItem> extends LinearLayout implements OnClickListener{

	private int position;
	
	private T item;
	private ItemClickHandler<T> itemClickHandler;

	public ImageTextView(Context context, AttributeSet attrs, int defStyle,int position,ItemClickHandler<T> itemClickedHandler) {
		super(context, attrs, defStyle);
		createCustomeView(context, R.layout.image_text_view,position,itemClickedHandler);
	}
	public ImageTextView(Context context, AttributeSet attrs,int position,ItemClickHandler<T> itemClickedHandler) {
		super(context, attrs);
		createCustomeView(context, R.layout.image_text_view,position,itemClickedHandler);
	}
	public ImageTextView(Context context,int position,ItemClickHandler<T> itemClickedHandler) {
		super(context);
		createCustomeView(context, R.layout.image_text_view,position,itemClickedHandler);
	}
	private void createCustomeView(Context context,int viewId,int position,ItemClickHandler<T> itemClickedHandler){
		this.position = position;
		this.itemClickHandler = itemClickedHandler;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(viewId, this);
        this.setOnClickListener(this);
        applyDefaultImage();
        applyDefaultText();
	}
	private void applyDefaultImage(){
		ImageView bmImage = (ImageView)findViewById(R.id.itemImage);
		ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
		if(bmImage != null){
			bmImage.setImageResource(R.drawable.image_not_available);
			progressBar.setVisibility(View.GONE);
			bmImage.setVisibility(View.VISIBLE);
		}
		
	}
	private void applyDefaultText(){
		TextView textView = (TextView)findViewById(R.id.itemHeading);
		if(textView != null){
			textView.setText(R.string.default_text_heading);	
		}
	}
	public void setHeadingText(String text){
		TextView textView = (TextView)findViewById(R.id.itemHeading);
		if(textView != null){
			textView.setText(text);	
			textView.setEllipsize(TruncateAt.END);
			textView.setMaxLines(2);
		}
		
	}
	public void setImageUrl(String url){
		ImageView bmImage = (ImageView)findViewById(R.id.itemImage);
		//ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
		//new DownloadImageTask(bmImage).execute(url);
		ImageLoader.getInstance().displayImage(url, bmImage, ImageUtil.getDisplayImageOptions());
		
	}
	public void setItem(T oneItem){
		item = oneItem;
		setHeadingText(oneItem.getTitle());
		setImageUrl(oneItem.getImageUrl());
	}
	@Override
	public void onClick(View v) {
		if(item == null){
			//this means user have clicked empty boxes
			return;
		}
		/*
		CharSequence text = "Clicked on "+ item.getTitle();
		int duration = Toast.LENGTH_SHORT;
		Toast toast = Toast.makeText(getContext(), text, duration);
		toast.show();	
		*/
		itemClickHandler.itemClicked(position,item);
	}
	

}
